package com.todod.base.module.base.controller.admin.protocols;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.protocols.vo.*;
import com.todod.base.module.base.dal.dataobject.protocols.ProtocolsDO;
import com.todod.base.module.base.service.protocols.ProtocolsService;

@Tag(name = "管理后台 - 协议标准")
@RestController
@RequestMapping("/base/protocols")
@Validated
public class ProtocolsController {

    @Resource
    private ProtocolsService protocolsService;

    @PostMapping("/create")
    @Operation(summary = "创建协议标准")
    public CommonResult<Long> createProtocols(@Valid @RequestBody ProtocolsSaveReqVO createReqVO) {
        return success(protocolsService.createProtocols(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新协议标准")
    public CommonResult<Boolean> updateProtocols(@Valid @RequestBody ProtocolsSaveReqVO updateReqVO) {
        protocolsService.updateProtocols(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除协议标准")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteProtocols(@RequestParam("id") Long id) {
        protocolsService.deleteProtocols(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得协议标准")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ProtocolsRespVO> getProtocols(@RequestParam("id") Long id) {
        ProtocolsDO protocols = protocolsService.getProtocols(id);
        return success(BeanUtils.toBean(protocols, ProtocolsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得协议标准分页")
    public CommonResult<PageResult<ProtocolsRespVO>> getProtocolsPage(@Valid ProtocolsPageReqVO pageReqVO) {
        PageResult<ProtocolsDO> pageResult = protocolsService.getProtocolsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProtocolsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出协议标准 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProtocolsExcel(@Valid ProtocolsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProtocolsDO> list = protocolsService.getProtocolsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "协议标准.xls", "数据", ProtocolsRespVO.class,
                        BeanUtils.toBean(list, ProtocolsRespVO.class));
    }

}